import axios from 'axios'
import {
  Message
} from 'element-ui'
import router from '../router'
import store from '@/vuex/store'
import qs from 'qs'

// const baseUrl = store.state.apiUrl
let baseUrl = store.state.baseUrl;
// switch (process.env.NODE_ENV) {
//   case "development": //生产环境
//     // baseUrl = "/ZHU";

// baseUrl = "http://jx.vaiwan.com/web/";
// baseUrl = "http://wz-wood.caizhiku.cn/web/";
//     break;
//   case "test": //测试环境
//     baseUrl = "http://jx.vaiwan.com/web/";
//     break;
//   default:
//     // baseUrl = "http://jx.vaiwan.com/web/";
//     baseUrl = "http://wz-wood.caizhiku.cn/web/";
// }
const rightCode = 200 //正确返回code
const noLogin = 401 //未登录返回code
function isLogin() { //未登录
  const fullPath = router.history.current.path
  if (fullPath == '/') {
    return
  }
  Message({
    showClose: true,
    message: '请重新登录',
    type: 'error'
  })
  window.localStorage.clear()
  router.replace({
    path: '/'
  })
}
var townId = window.localStorage.userList
  ? JSON.parse(window.localStorage.userList).townId
			: 1;
if (townId == 0) {
  townId = 1;
}
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  headers: {
    "townId": townId,
    "channel": 'console'//console 后台 zzd 浙争厅
  }
  // withCredentials: true
})
const instance2 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  withCredentials: false,
  headers: {
    "Content-Type": 'application/json',
    "townId": townId,
    "channel": 'console'//console 后台 zzd 浙争厅
  }
})
const instance3 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  responseType: "blob",
  headers: {
    "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8',
    "channel": 'console',
    "townId": townId,
  }
})
const instance4 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  responseType: "blob",
  headers: {
    "Content-Type": 'application/json',
    "townId": townId,
    "channel": 'console'
  }
})
//导入用 arraybuffer格式
const instance5 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  responseType: "arraybuffer",
  headers: {
    "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8',
    "townId": townId,
    "channel": 'console'
  }
})

function changeLoading(bl) { //开关加载动画
  store.state.loadingShow = bl
}



instance.interceptors.request.use(config => {
  try {
    config.headers.common['channel'] = 'console';
    var userList = window.localStorage.userList;
    var token = '';
    if(userList) {
      userList = JSON.parse(userList);
      token = userList.token;
    }
    // var token = store.state.token ? store.state.token : ''
    if (token) {
      config.headers.common['token'] = token;
    }
  } catch (e) {
    throw e
  }
  return config
}, err => {
  changeLoading(false) //关闭加载动画
  Message.error('请求超时');
  return Promise.reject(err);
})
instance2.interceptors.request.use(config => {
  try {
    config.headers.common['channel'] = 'console';
    var token = store.state.token ? store.state.token : ''
    if (token) {
      config.headers.common['token'] = token;
    }
  } catch (e) {
    throw e
  }
  return config
})
instance3.interceptors.request.use(config => {
  try {
    config.headers.common['channel'] = 'console';
    var token = store.state.token ? store.state.token : ''
    if (token) {
      config.headers.common['token'] = token;
    }
  } catch (e) {
    throw e
  }
  return config
})
instance4.interceptors.request.use(config => {
  try {
    var token = store.state.toke
    config.headers.common['channel'] = 'console'; n ? store.state.token : ''
    if (token) {
      config.headers.common['token'] = token;
    }
  } catch (e) {
    throw e
  }
  return config
})
instance5.interceptors.request.use(config => {
  try {
    config.headers.common['channel'] = 'console';
    var token = store.state.token ? store.state.token : ''
    if (token) {
      config.headers.common['token'] = token;
    }
  } catch (e) {
    throw e
  }
  return config
})

instance.interceptors.response.use(
  response => {

    changeLoading(false) //关闭加载动画
    if (response.data instanceof Blob) {
      return Promise.resolve(response)
    }
    if (response.data.code == rightCode) { //返回值正确返回值
      return response.data
    } else if (response.data.code == noLogin) {
      // if (response.data.msg) {
      //   Message({
      //     showClose: true,
      //     message: response.data.msg,
      //     type: 'error'
      //   })
      // }
      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      if (response.data.msg) {
        Message({
          showClose: true,
          message: response.data.msg,
          type: 'error'
        })
      }
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    switch (error.response.status) {
      case 401:
        // 返回 401 清除token信息并跳转到登录页面
        isLogin()
    }
    return Promise.reject(error)
  }
)
instance2.interceptors.response.use(
  response => {
    changeLoading(false) //关闭加载动画
    if (response.data.code == rightCode) { //返回值正确返回值
      return response.data
    } else if (response.data.code == noLogin) {
      // if (response.data.msg) {
      //   Message({
      //     showClose: true,
      //     message: response.data.msg,
      //     type: 'error'
      //   })
      // }
      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      if (response.data.msg) {
        Message({
          showClose: true,
          message: response.data.msg,
          type: 'error'
        })
      }
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    switch (error.response.status) {
      case 401:
        // 返回 401 清除token信息并跳转到登录页面
        isLogin()
    }
    return Promise.reject(error)
  }
)

export default {
  baseUrl,
  get(
    url,
    param,
    resParams = {
      notify: true
    }, data = {
      loading: true
    }, //是否开启loading
  ) {
    if (data.loading == true) { //post接口传入{loading:true}
      changeLoading(true)
    }
    return new Promise((resolve, reject) => {
      instance
        .get(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  put(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .put(url, param)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  delete(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .delete(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  post(
    url,
    param,
    resParams = {
      notify: true
    },
    data = {
      loading: false
    }, //是否开启loading
  ) {
    if (data.loading == true) { //post接口传入{loading:true}
      changeLoading(true)
    }
    return new Promise((resolve, reject) => {
      instance
        .post(url, qs.stringify(param), {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //对象提交
  JSON_Post(
    url,
    param,
    resParams = {
      notify: true
    },
    baseUrl = baseUrl,
  ) {
    changeLoading(true)
    return new Promise((resolve, reject) => {
      instance2
        .post(url, param, {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导入
  arraybuffer_post(
    url,
    param,
    resParams = {
      notify: true
    },
    data = {
      loading: false
    }, //是否开启loading
  ) {

    // if (data.loading == true) { //post接口传入{loading:true}
    //   changeLoading(true)
    // }
    return new Promise((resolve, reject) => {
      instance5
        .post(url, param, {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导入
  arraybuffer_get(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance5
        .get(url, {
          params: param,
          resParams: resParams
        }
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出表单格式
  blob_get(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance3
        .get(url, {
          params: param,
          resParams: resParams
        }
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出表单格式
  blob_post(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance3
        .post(url, qs.stringify(param)
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出JSON格式 数组传递
  blob_post_json(
    url,
    param,
    resParams = {
      notify: true
    },
    baseUrl = baseUrl,
  ) {

    // changeLoading(true)
    return new Promise((resolve, reject) => {
      instance4
        .post(url, param, resParams)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出JSON格式 数组传递
  blob_get_json(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance4
        .get(url, {
          params: param,
          resParams: resParams
        }
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },

}
